package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.app.AppInfo;
import com.ddxz.middleground.master.dto.app.AppPage;
import com.ddxz.middleground.master.service.ApplicationService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.annotation.DdxzRequestParam;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/30
 */

@RestController
@Api(tags = "应用管理")
public class ApplicationController {
    @ApiOperation(value = "检索应用", consumes = "application/json", produces = "application/json",
            notes = "根据用户指定的检索条件，分页检索应用信息")
    @RequestMapping(value = "/app/list", method = RequestMethod.GET)
    public DdxzResponse appList(@DdxzRequestHeader MasterHeader header, @DdxzRequestParam AppPage page) {
        return service.getAppList(header, page);
    }

    @ApiOperation(value = "查看应用", consumes = "application/json", produces = "application/json",
            notes = "根据应用标识，获取应用详情")
    @RequestMapping(value = "/app/{appId}/detail", method = RequestMethod.GET)
    public DdxzResponse appDetail(@DdxzRequestHeader MasterHeader header, @PathVariable("appId") String appId) {
        return service.getAppDetail(header, appId);
    }

    @ApiOperation(value = "保存应用", consumes = "application/json", produces = "application/json",
            notes = "将应用信息保存到数据库中，如果应用ID为空则新增，否则修改已有应用信息")
    @RequestMapping(value = "/app/save", method = RequestMethod.POST)
    public DdxzResponse appSave(@DdxzRequestHeader MasterHeader header, @RequestBody AppInfo appInfo) {
        return service.appSave(header, appInfo);
    }

    @ApiOperation(value = "删除应用", consumes = "application/json", produces = "application/json",
            notes = "删除指定的应用，如果该应用下有菜单则不允许删除")
    @RequestMapping(value = "/app/{appId}/remove", method = RequestMethod.GET)
    public DdxzResponse removeApp(@DdxzRequestHeader MasterHeader header, @PathVariable("appId") String appId) {
        return service.removeApp(header, appId);
    }

    private final ApplicationService service;

    public ApplicationController(ApplicationService service) {
        this.service = service;
    }
}
